package scripts;
import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class Stouts extends ScriptMethods implements Runnable {

	private boolean buy;

	public void start() {

		getAccountSettings();
		System.gc();

		Object[] options = {"Buy stouts (Falador)", "Train on dummies (Varrock)"};
		String res1 = (String)JOptionPane.showInputDialog(null, "", "Stouts", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

		if (res1.equals("Buy stouts (Falador)")) {
			buy = true;
		} else {
			buy = false;
		}

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptThread = null;
		scriptRunning = false;
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();

				if (invContains(620)) {
					dropItem(620);
					wait(random(620, 800));
				}

				if (buy) {
					if (bankOpen()) {
						depositAll(269);
						wait(random(610, 800));
						closeBank();
						wait(610);
					}
					if (questMenuOpen()) {
						answerQuestMenuStringContains("Stout", true);
						answerQuestMenuStringContains("access", true);
					}
					if (ourPlayerX() >= 328) {
						if (getInventorySize() == 30) {
							if (!bankOpen()) {
								openBank();
								wait(2000);
							}
						} else {
							walkToWait(317, 549);
						}
					} else {
						if (getInventorySize() == 30) {
							walkToWait(330, 553);
						} else {
							talkToNpc(new int[] { 142 });
							wait(4000);
						}
					}
				} else {
					int[] bankDoor = getObjectById(64);
					if (bankDoor[0] != -1 && !bankOpen()) {
						atObject(bankDoor[1], bankDoor[2]);
						wait(random(1000, 1200));
					}
					if (bankOpen()) {
						if (invContains(269)) {
							depositAll(269);
							wait(random(610, 800));
						}
						if (invContains(620)) {
							depositAll(620);
							wait(random(610, 800));
						}
						withdraw(269, 25);
						wait(random(610, 800));
						closeBank();
						wait(600);
					}
					if (invContains(269)) {
						if (getCurrentLevel()[0] > 7) {
							useItem(269);
							wait(random(620, 800));
						} else {
							atObject(106, 504);
							wait(random(610, 800));
						}
					} else {
						if (ourPlayerY() >= 510) {
							openBank();
							wait(2000);
						} else {
							walkToWait(103, 510);
						}
					}
				}
				wait(random(50, 100));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (!isLoggedIn())
			doLogin();
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
	}
}